/**
 * 
 */
package uniandes.moviles.antid.mundo;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author Julio Mendoza
 * A user of the application. It can be either a patient or a professional
 */
public abstract class User 
{
	/**
	 * The identifier of the user inside of the application.
	 */
	public String userName;
	
	/**
	 * Password of the user
	 */
	public String password;
	
	/**
	 * the name of the user.
	 */
	public String name;
	
	/**
	 * The role of the user.
	 */
	private int role;
	
	/**
	 * Manager to consult and update the database.
	 */
	private PersistenceManager manager;
	
	/**
	 * Creates a new application user.
	 * @param name The real name of the user.
	 * @param userName The user name the user will use to identify himself inside the application.
	 * @param password The user password.
	 * @param pass2 The user password confirmation.
	 * @param manager The persistence manager to e able to use the data base.	 
	 */
	public User(PersistenceManager manager)
	{
		this.name = "";
		this.password = "";
		this.userName = "";
		this.role = -1;
		this.manager = manager;
	}
	
	
	/**
	 * Registers a new user in the system.
	 * @param userName The user name of the user to register.
	 * @param pass1 The new password the user enters.
	 * @param pass2 the password confirmation.
	 * @param role The role of he user (i. e. Professional or Patient);
	 * @throws Exception If:
	 * <ul>
	 * <li>The confirmation is different from the password to assign.</li>
	 * <li>The password length is shorter than 8 characters.</li>
	 * <li>The user name is already in use by another user.</li>
	 * <li>The user name length is less than six characters.</li>
	 * </ul>
	 */
	public void register(String userName, String pass1, String pass2, String name, int role) throws Exception
	{
		SQLiteDatabase db = manager.getReadableDatabase();
		String[] columns = {"id"};
		Cursor c = db.query(PersistenceManager.TABLE_USERS, columns, "username like '"+userName+"'", null, null, null, null);
		db.close();
		if(userName.length()<6)
		{
			throw new Exception("The user name must have at least 6 characters.");
		}
		else if(pass1.length()<8)
		{
			throw new Exception("The password must have at least 8 characters.");
		}
		else if(!pass1.equals(pass2))
		{
			throw new Exception("The password confirmation and the password must coincide");
		}		
		else if(!c.isNull(0))
		{
			throw new Exception("The user name " + userName + " is already in use.");
		}
		else if(name.equals(""))
		{
			throw new Exception("The name field must be filled.");
		}
		else
		{
			db = manager.getWritableDatabase();
			String sql = "INSERT INTO " + PersistenceManager.TABLE_USERS + "(username, password, name, role) VALUES ('" + userName + "', '" + pass1+ "', '" + name+ "',"+ role +")";
			db.execSQL(sql);
			db.close();
		}
		
	}
	
	/**
	 * Authenticates the user in the application to access its services, the services depend on the role of the user.
	 * @param userName The user name of the user to log.
	 * @param password The password of the user.
	 * @return role The code of the role of the user.
	 * @throws Exception if the fields are wrong or the user does not exist in the database.
	 */
	public int login(String userName, String password) throws Exception
	{
		int role = -1;
		if(userName.equals("")||password.equals(""))
		{
			throw new Exception("The fields user name and password must be filled");
		}
		else
		{
			SQLiteDatabase db = manager.getReadableDatabase();
			String[] columns = {"id"};
			String selection = "username like '"+ userName + "' AND password like '" + password + "'";
			Cursor c = db.query(PersistenceManager.TABLE_USERS, columns, selection, null, null, null, null);			
			if(!c.moveToFirst())
			{
				throw new Exception("The user name or password are incorrect.");
			}
			else
			{
				role = c.getInt(0);
			}
			
		}
		return role;
	}
	
	
	
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}


	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}


	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}


	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * Returns the role of the user. The possible values of the role code are:
	 * <ul>
	 * <li> 1 if the user is a professional. </li>
	 * <li> 0 if the user is a patient. </li>
	 * </ul>
	 * @return role
	 */
	public int getRole()
	{
		return role;
	}
	
	
	
}
